package com.ourteam.system.api;

import com.ourteam.frame.web.AbstractTreeNodeBuilder;
import com.ourteam.system.dao.SysModuleDAOQueryBean;
import com.ourteam.system.domain.ApplicationDomain;
import com.ourteam.system.service.ISystemApplicationService;
import com.ourteam.system.service.SystemApplicationServiceFactory;

public class SystemDomainNodeBuilder extends AbstractTreeNodeBuilder<Object, ApplicationDomain> {

	private final static ISystemApplicationService applicationService = SystemApplicationServiceFactory
			.getSystemApplicationService();

	private long domainId;

	public long getDomainId() {
		return domainId;
	}

	public void setDomainId(long domainId) {
		this.domainId = domainId;
	}

	@Override
	public ApplicationDomain[] getTreeNodeObjects(Object parentObject) throws Exception {
		System.out.println(this.domainId);
		if (this.domainId != 0) {
			return new ApplicationDomain[] { applicationService.getApplicationDomainById(this.domainId) };
		} else if (parentObject != null) {
			return new ApplicationDomain[] {
					applicationService.getApplicationDomainById(Long.parseLong(parentObject.toString())) };
		} else {
			return applicationService.getApplicationDomains();
		}

	}

	@Override
	public Object getNodeId(ApplicationDomain childObject) {
		return childObject.getId();
	}

	public boolean isLeaf(ApplicationDomain childObject) {
		return false;
	}

	@Override
	public String getNodeDefineId(ApplicationDomain childObject) throws Exception {
		return "sysDomain";
	}

	@Override
	public Object converToParentObject(Object parent) throws Exception {
		return parent;
	}

	@Override
	public int getChildCount(ApplicationDomain childObject) throws Exception {
		SysModuleDAOQueryBean queryBean = new SysModuleDAOQueryBean();

		SysModuleDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andDomainIdEqualTo(childObject.getId()).andParentModuleIdEqualTo(0);

		return applicationService.querySystemModules(queryBean).length;
	}

}
